import geom.Vector;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

import processing.core.*;
public class Space extends Rectangle{
	Edge edge;
	public ArrayList anchors;
	
	public Space(float x, float y, float w, float h, Edge e){
		super(x, y, w, h);
		edge = e;
	}
	public Space(float x, float y, float w, float h){
		super(x, y, w, h);
	}
	public Space(){
		super();
	}
	
	public void setAnchors(ArrayList anchors){
		Iterator a = anchors.iterator();
		while(a.hasNext()){
			Anchor anchor = (Anchor) a.next();
			if(onEdge(anchor))
				this.anchors.add(anchor);
		}
	}
	
	public Anchor getAnchor(int i){
		return (Anchor)anchors.get(i);
	}
	
	public void removeAnchor(int i){
		anchors.remove(i);
	}
	
	private class Comp implements Comparator{
		int dir;
		public Comp(Edge e){
			dir = e.dir;
		}
		public int compare(Object arg0, Object arg1) {
			Space p1 = (Space)arg0;
			Space p2 = (Space)arg1;
			if(dir == Edge.HORIZONTAL)
				return (int)Math.floor(p2.x - p1.x);
			else
				return (int)Math.floor(p2.y - p1.y);
		}
		
	}

	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("Space ID: " + id + " pos " + x + " " + y + " size " + w + " " + h);
		return sb.toString();
	}
	
	public boolean onEdge(Vector v){
		if(v.x == x || v.x == x + w)
			if(v.y >= y && v.y <= y + h) return true;
		if(v.y == y || v.y == y + h)
			if(v.x >= x && v.x <= x + w) return true;
		return false;
		
	}

	
	
}

